package utils

import (
	"github.com/project-flogo/core/data/coerce"

	"github.com/project-flogo/core/data"
	"github.com/project-flogo/core/data/expression/function"
)

func init() {
	err := function.Register(&fnXorChecksum{})
	if err != nil {
		panic(err)
	}
}

type fnXorChecksum struct{}

func (fnXorChecksum) Name() string {
	return "xorChecksum"
}

func (fnXorChecksum) Sig() (paramsTypes []data.Type, isVariadic bool) {
	return []data.Type{data.TypeBytes}, false
}

func (fnXorChecksum) Eval(params ...interface{}) (interface{}, error) {
	b, err := coerce.ToBytes(params[0])
	if err != nil {
		return 0, err
	}
	sum := XorCheck(b)
	return sum, nil 
}
